package cn.itcast.bos.action.base;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.utils.BaseAction;
import cn.itcast.crm.domain.Customer;

public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;

	@Action(value = "fixedArea_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String saveFixedArea() {
		fixedAreaService.save(model);
		return SUCCESS;
	}

	@Action(value = "fixedArea_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 创建分页查询对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 创建条件查询对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 创建保存查询条件的集合
				Set<Predicate> hashSet = new HashSet<Predicate>();
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.like(root.get("id").as(String.class), model.getId());
					hashSet.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), model.getCompany());
					hashSet.add(p2);
				}
				return cb.and(hashSet.toArray(new Predicate[0]));
			}
		};

		Page<FixedArea> page = fixedAreaService.pageQuery(specification, pageable);

		pushPageDataToValueStack(page);
		return SUCCESS;
	}

	// 查询未关联定区的客户
	@Action(value = "fixedArea_findNoAssociationFixedAreaCustomers", results = {
			@Result(name = "success", type = "json") })
	public String findNoAssociationCustomer() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);

		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 查询已关联定区的客户
	@Action(value = "fixedArea_findHasAssociationFixedAreaCustomers", results = {
			@Result(name = "success", type = "json") })
	public String findHasAssociationCustomer() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
						+ model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);

		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 属性注入要关联到定区的客户ids
	//customerIds与关联客户窗口中的name属性一致
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	// 关联客户到定区
	@Action(value = "fixedArea_associationCustomerToFixedArea", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String associationCustomerToFixedArea() {
		String customerIdStr = StringUtils.join(customerIds, "-");
		
		WebClient.create("http://localhost:9002/crm_management/services/customerService/associationcustomertofixedarea?"
				+ "customerIdStr=" + customerIdStr + "&fixedAreaId=" + model.getId()).put(null);
		return SUCCESS;
	}
	
	//属性注入关联快递员获得 的  快递员id 和收牌时间id
	private Integer courierId;
	private Integer takeTimeId;
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value="fixedArea_associationCourierToFixedArea",results={
			@Result(name="success", type = "redirect", location = "./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
}
